package com.lyc.controller;

import com.github.pagehelper.PageInfo;
import com.lyc.pojo.Users;
import com.lyc.service.UserService;
import com.lyc.tool.AjaxResponse;
import com.lyc.tool.Msg1;
import com.lyc.tool.SYS;
import com.lyc.tool.SendSms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@Controller
@RequestMapping("/user")//请求路径
@CrossOrigin

public class UserController {
    /*属性区*/
    @Autowired
    private UserService userService;//私有属性
    private Integer captcha = -1;


    /*方法区*/
    @ResponseBody
    @RequestMapping(value = "/tel/{tel}",method = RequestMethod.GET)//获取手机验证码
    public int getCode(@PathVariable("tel") String tel,HttpSession session){
        Random random = new Random();
        int i = random.nextInt(899999) + 100000;
        captcha = SendSms.test(tel, i);
        session.setAttribute(SYS.SYS_CODE,captcha);
        return captcha;
    }
    /*方法区*/
    //用户登录
    @ResponseBody
    @RequestMapping("/login")
    public Users login(String utel,String upass,HttpSession session){

        Users user= userService.login(utel,upass);
    /*    if(user!=null){
            session.setAttribute("uid",user.getUid());
            session.setAttribute("isUser",1);
            session.setAttribute("uname",user.getUname());
        }*/
        session.setAttribute(SYS.SYS_USER,user);//session传值
        return user;
    }

    @RequestMapping("testSession")
    public String test1(HttpSession session){
        Users users = (Users)session.getAttribute(SYS.SYS_USER);
      //  int code = (int)session.getAttribute(SYS.SYS_CODE);
        System.out.println(users);
       // System.out.println(code);
        return "hello";
    }
    @ResponseBody//用户注册
    @RequestMapping(value = "/registerUser/{utel}/{captcha1}",method = RequestMethod.POST)
    public Users regist(@PathVariable("utel") String utel,@PathVariable("captcha1")int captcha1,HttpSession session) {
        /*Integer captcha = (Integer) session.getAttribute(SYS.SYS_CODE);
        System.out.println(captcha+"=======");*/
        if(userService.selectTel(utel)==null){ //判断该手机是否已注册
            if(captcha1==captcha){  //判断验证码是否正确
                Users users = new Users();
                users.setUname(UUID.randomUUID().toString().substring(0,12));//默认用户名
                users.setUpass(utel);//首次注册用户默认密码为手机号
                users.setUrole(4);//首次注册用户角色权限为4级
                users.setUtel(utel);//
                int i = userService.regist(users);//用户注册
                if(i==1){

                    Users users1 = userService.login(utel, "123456");
                    session.setAttribute(SYS.SYS_USER,users1);
                    return users1;//查询刚注册用户
                }
            }
        }
        return null;
    }
    @ResponseBody
    @RequestMapping("/remove")//用户注销
    public int remove(HttpSession session){
        session.invalidate();
        System.out.println("用户已注销");
        return 1;
    }

    @RequestMapping("/updatePass/{uid}/{upass}")//修改密码
    @ResponseBody
    public int update(@PathVariable("uid") Integer uid,@PathVariable("upass")String upass){

        return userService.update(uid,upass);
    }



    /*后台代码*/
    @RequestMapping("/houtailogin")

    public String login(Users user,HttpSession session){

        System.out.println(user.getUname());
        Users user2 = userService.houtailogin(user.getUname());
        if(user.getUpass().equals(user2.getUpass())){

           // model.addAttribute(SYS.SYS_USER,user2);
            //model.addAttribute(SYS.LOGIN_ERROR,"erooor.....");
            session.setAttribute(SYS.SYS_USER,user2);
            return "/admin";
        }else {
            return "/adminLogin";
        }
    }


    @RequestMapping("/all")
    @ResponseBody
    public Msg1<Users> selectAll(@RequestParam(defaultValue = "1")Integer page,
                                 @RequestParam(defaultValue = "5")Integer limit){
        List<Users> users = userService.selectAll();
        PageInfo<Users> pageInfo=userService.getUsersByPage(page,limit);
        // System.out.println(pageInfo);
        Msg1<Users> msg=new Msg1<>();
        //count属性类型改为long值
        msg.setCount(pageInfo.getTotal());
        msg.setCode(0);
        msg.setData(pageInfo.getList());
        return msg;
    }
    @RequestMapping("/del/{id}")
    @ResponseBody
    public AjaxResponse delete(@PathVariable("id") int id){
        System.out.println(id+"............................");
        userService.delete(id);
        return new AjaxResponse(true,"删除成功！");
    }

    @RequestMapping(value = "/insert" )
    @ResponseBody
    public AjaxResponse insert (Users users){
        //System.out.println(users);
        int i =userService.insert(users);
        System.out.println(i);
        // return "redirect:/admin/admin/goods";
        return new AjaxResponse(true,"加入成功！");
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public AjaxResponse update(Users users ) {
        int update = userService.update(users);
        if (update == 1) {
            return new AjaxResponse(true, "删除成功！");
        }else {
            return new AjaxResponse(false, "删除失败！");
        }
    }

    @RequestMapping(value = "/deleteSelect",method = RequestMethod.GET)
    @ResponseBody
    public AjaxResponse deleteSelect(@RequestParam(name = "idArry", required = true) String idArry){
        String[] idlist = idArry.split(",");
        List<String> list=new ArrayList<>();
        for (int i=0;i<idlist.length;i++){
            list.add(idlist[i]);
        }
        int deleteSelect = userService.deleteSelect(list);
        System.out.println(deleteSelect);
        return new AjaxResponse(true,"删除成功！");
    }
}
